package models

import (
	"github.com/jinzhu/gorm"
	"kop/modules/maid/entity"
	"kop/pb/service"
	"kop/util/cd"
	"kop/util/date"
	"time"
)

const (
	DateNum    = 3   // 每日约会次数
	BarLuck    = 80  // 每日初始幸运
	BarMaxLuck = 100 // 幸运最大值
	DateCD     = 30 * time.Minute
	DrinkCD    = 30 * time.Minute
)

func GetMaidDate(dB *gorm.DB, roleID int32, vip *service.VIP) entity.MaidDate {

	var today = date.Date(time.Now())

	var data = entity.MaidDate{
		RoleID:    roleID,
		DateNum:   DateNum,
		Date:      today,
		BarLuck:   BarLuck,
		DateTime:  cd.NewCD(DateCD, vip.Date).SetNum(vip.Date),
		DrinkTime: cd.NewCD(DrinkCD, vip.Drink).SetNum(vip.Drink),
	}

	if err := dB.FirstOrCreate(&data).Error; err != nil {
		panic(err)
	}

	if today != data.Date {

		data.Date = today
		data.DateNum = DateNum
		data.BarLuck = BarLuck
		data.BarWishNum = 0
	}

	return data
}

func SaveMaidDate(db *gorm.DB, data *entity.MaidDate) {

	if err := db.Save(data).Error; err != nil {
		panic(err)
	}
}
